
layui.use(['element','jquery','form','table','layer','laydate','layedit'],function () {
    var element = layui.element;
    var $=layui.jquery;
    var form=layui.form;
    var table=layui.table;
    var layer=layui.layer;
    var laydate=layui.laydate;
    var layedit=layui.layedit;

    //日期时间选择器
    laydate.render({
        elem: '#startTime'
        ,type: 'datetime'
    });
    //日期时间选择器
    laydate.render({
        elem: '#endTime'
        ,type: 'datetime'
    });

    var tableIns = table.render({
        elem: '#noticeTable',
        url: '/admin/notice/noticeList',
        toolbar: '#noticeToolBar',
        page: 'true',
        title: '公告数据表',
        cols: [ [
            {type:'checkbox'},
            {field:'id',title:'ID',align:'center'},
            {field:'title',title:'公告标题',align:'center'},
            {field:'summary',title:'公告摘要',align:'center'},
            {field:'visit',title:'点击量',align:'center'},
            {field:'nickName',title:'发布人',align:'center'},
            {field:'createTime',title:'创建时间',align:'center'},
            {field:'changeTime',title:'修改时间',align:'center'},
            {title:'操作',toolbar:'#noticeRowBar',width:250}
        ] ],
        done:function (res,curr,count) {
            if(curr>1 && res.data.length==0){
                tableIns.reload({
                    page:{curr:curr-1}
                });

            }
        }
    });



    form.on("submit(doSearch)",function (data) {
        tableIns.reload({
            url:'/admin/notice/noticeList',
            where:data.field,
            page:{
                curr:1
            }
        })
        return false;
    })

    table.on('toolbar(noticeTable)',function (obj) {
        switch (obj.event) {
            case "add":
                openAddNoticeLayer();
                break;
            case "batchDelete":
                batchDelete();
                break;
        }
    })
    table.on('tool(noticeTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case "update":
                openUpdateNoticeLayer(data);
                break;
            case "delete":
                deleteInfo(data);
                break;
            case "show":
                showNotice(data);
                break;
        }
    })

    //弹出层索引
    var mainIndex;
    //提交路径
    var url;
    //初始化富文本编辑器
    var editIndex;

    //查看公告
    function  showNotice(data) {
        mainIndex = layer.open({
            type:1,
            content:$("#showNoticeDiv"),
            area:["800px","500px"],
            title:"查看公告",
            success:function () {
                $("#show_title").html(data.title);
                $("#show_opername").html(data.nickName);
                $("#show_createtime").html(data.createTime);
                $("#show_content").html(data.content);
            }
        });
    }

    //添加
    function openAddNoticeLayer() {
        mainIndex = layer.open({
            type:1,
            content:$("#addOrUpdateDiv"),
            area:["900px","600px"],
            title:"发布公告",
            success:function () {

                $("#dataFrm")[0].reset();
                layedit.setContent(editIndex,"");
                url="/admin/notice/addNotice";

            }
        });

        //初始化富文本编辑器
        editIndex = layedit.build('content');
    }
    //修改
    function openUpdateNoticeLayer(data) {
        mainIndex = layer.open({
            type:1,
            content:$("#addOrUpdateDiv"),
            area:["900px","600px"],
            title:"更新公告",
            success:function () {
                form.val("dataFrm",data);
                layedit.setContent(editIndex,data.content);
                url="/admin/notice/updateNotice";

            }
        });

        //初始化富文本编辑器
        editIndex = layedit.build('content');
    }
    //删除
    function deleteInfo(data){
        layer.confirm("确定要删除这条数据吗？",{icon:3,title:"提示"},function (index) {
            $.post("/admin/notice/deleteById",{"id":data.id},function (result) {
                if(result.success){
                    tableIns.reload();
                }
                layer.msg(result.content);
            },"json")
            //关闭弹窗
            layer.close(index);
        })
    }

    //批量删除
    function batchDelete(){
        //获取选中的行
        var checkStatus = table.checkStatus("noticeTable");
        //判断是否选中行
        var length = checkStatus.data.length;
        if(length>0){
            layer.confirm('确定要删除这些数据吗?',{icon:3,title:"提示"},function (index) {
                var data = checkStatus.data;
                var idArr = []
                for(var i = 0 ; i<length;i++){
                    idArr.push(data[i].id);
                }
                var ids = idArr.join(",");
                $.post("/admin/notice/batchDelete",{"ids":ids},function (result) {
                    if(result.success){
                        tableIns.reload();
                    }
                    layer.msg(result.content);
                },"json")
            })
            layer.close(index);
        }else{
            layer.alert("请选择要删除的行",{icon:0})
        }
    }

    //提交事件
    form.on("submit(doSubmit)",function (data) {
        //同步富文本和textarea内用
        layedit.sync(editIndex);
        console.log($("#dataFrm").serialize())
        $.post(url,$("#dataFrm").serialize(),function (result) {
            if(result.success){
                //提示信息
                layer.msg(result.content,{icon:1});
                //刷新数据表格
                tableIns.reload();
                //关闭窗口
                layer.close(mainIndex);
            }else {
                layer.msg(result.content,{icon:2});
            }
        },"json");
        return false;
    })


});
